package com.example.book.in.controller;

import com.example.book.base.BaseController;
import com.example.book.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.example.book.in.entity.BookCategory;
import com.example.book.in.service.BookCategoryService;


/**
 * @author hcj
 * @date 2019-10-27 21:43:03
 */
@Controller
@RequestMapping(value = "/sys/in/bookCategory")
public class BookCategoryController extends BaseController {

    @Autowired
    private BookCategoryService bookCategoryService;

    @ModelAttribute
    public BookCategory get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            BookCategory bookCategory = new BookCategory();
            bookCategory.setId(id);
            return bookCategoryService.get(bookCategory);
        } else {
            return new BookCategory();
        }
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public BookCategory getById(BookCategory bookCategory) {
        return bookCategory;
    }

    /**
     * 新增修改页面
     *
     * @param bookCategory
     * @param model
     * @return
     */
    @GetMapping("/form")
    public String form(BookCategory bookCategory, Model model) {
        if (bookCategory != null) {
            model.addAttribute("bookCategory", bookCategory);
        }
        return "in/category/category_form";
    }

    /**
     * 分类列表页面
     *
     * @param bookCategory
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(BookCategory bookCategory) {
        return "in/category/category_list";
    }

    /**
     * 分类列表数据
     *
     * @param bookCategory
     * @return
     */
    @RequestMapping(value = "/info")
    @ResponseBody
    public List<BookCategory> info(BookCategory bookCategory) {
        return bookCategoryService.findList(bookCategory);
    }

    /**
     * 分类列表分也数据
     *
     * @param bookCategory
     * @return
     */
    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<BookCategory> findPage(BookCategory bookCategory) {
        return bookCategoryService.findPage(bookCategory);
    }

    /**
     * 分类信息修改删除
     *
     * @param bookCategory
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(BookCategory bookCategory) {
        bookCategoryService.save(bookCategory);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion", method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id) {
        bookCategoryService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids) {
        bookCategoryService.delete(ids);
        return Result.success();
    }

}